import 'dart:convert';

class SnippetPageEntity {
  int? total;
  late Map<String, List<SnippetModel>> todoMaps;
  SnippetPageEntity({required this.total, required this.todoMaps});
  SnippetPageEntity.fromJson(Map<String, dynamic> json) {
    if (json['list'] != null) {
      List<SnippetModel> list = [
        ...(json['list'] as List? ?? []).map((o) => SnippetModel.fromJson(o))
      ];
      todoMaps = toListMap(list);
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['todoMaps'] = todoMaps;
    data['total'] = total;
    return data;
  }

  @override
  String toString() => jsonEncode(toJson());
}

Map<String, List<SnippetModel>> toListMap<T>(List<SnippetModel> list) {
  late Map<String, List<SnippetModel>> todoMap = {};

  var keys = list.map<String>((e) => e.month!).toSet();
  for (final key in keys) {
    var values = list.where((element) => element.month == key);
    todoMap[key] = values.toList();
  }
  return todoMap;
}

class SnippetEntity {
  late SnippetModel snippetModel;
  SnippetEntity({required this.snippetModel});
  SnippetEntity.fromJson(Map<String, dynamic> json) {
    if (json['code'] != null) {
      snippetModel = SnippetModel.fromJson(json['data']);
    }
  }
}

class SnippetModel {
  int? snippetId;
  String? title;
  String? content;
  String? intro;
  String? type;
  String? tags;
  String? month;
  String? status;
  bool? delFlag;
  String? createBy;
  String? updateTime;
  String? remark;

  SnippetModel(
      {this.snippetId,
      this.title,
      this.content,
      this.intro,
      this.type,
      this.tags,
      this.month,
      this.status,
      this.delFlag,
      this.createBy,
      this.updateTime,
      this.remark});

  SnippetModel.fromJson(Map<String, dynamic> json) {
    snippetId = json['snippetId'];
    title = json['title'];
    content = json['content'];
    intro = json['intro'];
    type = json['type'];
    tags = json['tags'];
    month = json['month'];
    status = json['status'];
    delFlag = json['delFlag'];
    createBy = json['createBy'];
    updateTime = json['updateTime'];
    remark = json['remark'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['snippetId'] = snippetId;
    data['title'] = title;
    data['content'] = content;
    data['intro'] = intro;
    data['type'] = type;
    data['tags'] = tags;
    data['month'] = month;
    data['status'] = status;
    data['delFlag'] = delFlag;
    data['createBy'] = createBy;
    data['updateTime'] = updateTime;
    data['remark'] = remark;
    return data;
  }

  @override
  String toString() => jsonEncode(toJson());
}
